#include "Components/ComponentCreatureSounds.h"
#include "Subsystems/SubsystemTimeOfDay.h"
#include "GameSingleton.h"
#include "GameRegistry.hpp"
#include "Network/PackageManager.h"
#include "Network/Packages/ComponentBehaviorPackage.h"

namespace PocketSurvival
{
    void ComponentCreatureSounds::playIdleSound(bool skipIfRecentlyPlayed)
    {
        if(timeOfDay->getGameTime() > lastSoundTime + (skipIfRecentlyPlayed ? 12.0f : 1.0f))
        {
            lastSoundTime = timeOfDay->getGameTime();
            GameSingleton::packageManager->addSendPackageToQueue(std::make_shared<ComponentBehaviorPackage>(this, 0, skipIfRecentlyPlayed));
        }
    }
    void ComponentCreatureSounds::playPainSound()
    {
        if (timeOfDay->getGameTime() > lastSoundTime + 1.0)
        {
            lastSoundTime = timeOfDay->getGameTime();
            GameSingleton::packageManager->addSendPackageToQueue(std::make_shared<ComponentBehaviorPackage>(this, 1));
        }
    }
    void ComponentCreatureSounds::playMoanSound()
    {
        if (timeOfDay->getGameTime() > lastSoundTime + 1.0)
        {
            lastSoundTime = timeOfDay->getGameTime();
            GameSingleton::packageManager->addSendPackageToQueue(std::make_shared<ComponentBehaviorPackage>(this, 2));
        }
    }
    void ComponentCreatureSounds::playSneezeSound()
    {
        if (timeOfDay->getGameTime() > lastSoundTime + 1.0)
        {
            lastSoundTime = timeOfDay->getGameTime();
            GameSingleton::packageManager->addSendPackageToQueue(std::make_shared<ComponentBehaviorPackage>(this, 3));
        }
    }
    void ComponentCreatureSounds::playCoughSound()
    {
        if (timeOfDay->getGameTime() > lastSoundTime + 1.0)
        {
            lastSoundTime = timeOfDay->getGameTime();
            GameSingleton::packageManager->addSendPackageToQueue(std::make_shared<ComponentBehaviorPackage>(this, 4));
        }
    }
    void ComponentCreatureSounds::playPukeSound()
    {
        if (timeOfDay->getGameTime() > lastSoundTime + 1.0)
        {
            lastSoundTime = timeOfDay->getGameTime();
            GameSingleton::packageManager->addSendPackageToQueue(std::make_shared<ComponentBehaviorPackage>(this, 5));
        }
    }
    void ComponentCreatureSounds::playAttackSound()
    {
        if (timeOfDay->getGameTime() > lastSoundTime + 1.0)
        {
            lastSoundTime = timeOfDay->getGameTime();
            GameSingleton::packageManager->addSendPackageToQueue(std::make_shared<ComponentBehaviorPackage>(this, 6));
        }
    }

    static const std::string componentName = "CreatureSounds";
    const std::string &ComponentCreatureSounds::getName() const
    {
        return componentName;
    }
    const std::string &ComponentCreatureSounds::GetName()
    {
        return componentName;
    }
    void ComponentCreatureSounds::load(const nlohmann::json &jsonData)
    {
        componentCreature = GameSingleton::gameRegistry->get<ComponentCreature*>(entity);

        timeOfDay = GameSingleton::singletonPtr->getSingleton<SubsystemTimeOfDay>();
    }
} // namespace PocketSurvival
